package com.nondo.dean.order.service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.common.web.View;
import com.nondo.dean.order.model.ProductBomBean;
import com.nondo.dean.order.model.ProductMixBean;
import com.nondo.dean.order.model.param.ProductOrderParam;
import com.nondo.dean.order.model.result.ProductResult;

/**
 * 产品管理业务类
 * 
 * @author zhaoljin
 * @date 2018年4月16日
 */
@Component
public class ProductService {

    @Autowired
    RestTemplate restTemplate;

    /**
     * 根据产品ID获取产品信息
     * 
     * @param productId 产品ID
     * @return 产品信息{@link ProductResult}
     */
    public ProductResult getProductBeanById(Integer productId) {
        ProductResult productResult = new ProductResult();
        @SuppressWarnings("unchecked")
        View<LinkedHashMap<String, Object>> result =
            restTemplate.postForEntity("http://dean-product/product/v1/inner/product/info/" + productId, null, View.class)
                .getBody();
        if (result.getCode() == 20000 && result.getData() != null) {
            BeanUtils.copy(result.getData(), productResult);
        }
        return productResult;
    }

    /**
     * 根据多个产品ID值获取产品列表
     * @param ids 多个产品ID值
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<ProductResult> getProductBeanListByIds(String ids) {
        List<ProductResult> productResultList = new ArrayList<ProductResult>();
        if (StringUtils.isNotBlank(ids)) {
            View<List<ProductResult>> result =
                restTemplate.postForEntity("http://dean-product/product/v1/inner/product/list/" + ids, null, View.class)
                    .getBody();
            if (result.getCode() == 20000 && result.getData() != null) {
                productResultList = result.getData();
            }
        }
        return productResultList;
    }
    
    /**
     * 根据父级编码查询商品BOM列表
     * 
     * @param productCode 产品编号
     * @return 产品BOM列表信息List<{@link ProductBomBean}>
     */
    @SuppressWarnings("unchecked")
    public List<ProductBomBean> getProductBomListByParentCode(String productCode) {
        List<ProductBomBean> productBomList = new ArrayList<ProductBomBean>();
        View<List<ProductBomBean>> result =
            restTemplate.postForEntity("http://dean-product/product/v1/inner/product/bom/list/" + productCode, null,
                View.class).getBody();
        if (result.getCode() == 20000 && result.getData() != null) {
            productBomList = JSON.parseArray(JSON.toJSONString(result.getData()), ProductBomBean.class);
        }
        return productBomList;
    }
    
    /**
     * 减库存加销量
     * @param productId 产品ID
     * @param num 产品数量
     * @return 影响行数
     */
    public Integer updateStockSaleNum(ProductOrderParam prodcutParam) {
        HttpEntity<ProductOrderParam> httpEntity = new HttpEntity<ProductOrderParam>(prodcutParam);
        @SuppressWarnings("unchecked")
        View<Integer> result =
            restTemplate.postForEntity("http://dean-product/product/v1/inner/product/stock/update/", httpEntity,
                View.class).getBody();
        if (result.getCode() == 20000 && result.getData() != null) {
            return result.getData();
        }
        return null;
    }

    /**
     * 根据产品编号获取产品信息
     * 
     * @param productId 产品ID
     * @return 产品信息{@link ProductResult}
     */
    public ProductResult getProductBeanByCode(String productCode) {
        ProductResult productResult = new ProductResult();
        @SuppressWarnings("unchecked")
        View<LinkedHashMap<String, Object>> result =
            restTemplate.postForEntity("http://dean-product/product/v1/inner/product/get/" + productCode, null, View.class)
                .getBody();
        if (result.getCode() == 20000 && result.getData() != null) {
            BeanUtils.copy(result.getData(), productResult);
        }
        return productResult;
    }
    
    /**
     * 查找产品所参与的产品组合列表
     * @param productId 产品ID
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<ProductMixBean> getProductMixList(@PathVariable Integer productId) {
        List<ProductMixBean> productMixList = null;
        View<List<ProductMixBean>> result =
            restTemplate.postForEntity("http://dean-product/product/v1/inner/product/mix/list/" + productId, null,
                View.class).getBody();
        if (result.getCode() == 20000 && result.getData() != null) {
            productMixList = result.getData();
        }
        return productMixList;
    }

}
